using System.Collections;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TemporalProductEndTimeWindow : MonoBehaviour
{
	public static TemporalProductEndTimeWindow instance;

	private static string keyCountPoductEndTime = "TemporalProductEndTimeCount";

	private static string keyNeedShowWindow = "TemporalProductEndTimeNeedShowWindow";

	private static string keyPoductEndTime = "TemporalProductEndTimeKey_";

	public GameObject window;

	[SerializeField]
	private UILabel labelPriceArmor;

	[SerializeField]
	private UILabel labelPriceWeapon;

	[SerializeField]
	private UILabel labelNameArmor;

	[SerializeField]
	private UILabel labelNameWeapon;

	[SerializeField]
	private UIButton buttonArmor;

	[SerializeField]
	private UIWidget armorEquipped;

	[SerializeField]
	private UIButton buttonWeapon;

	[SerializeField]
	private UIWidget weaponEquipped;

	private Product productWeapon;

	private Product productArmor;

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Start()
	{
		if (SceneManager.GetActiveScene().name.Equals("Lobby"))
		{
			Show();
		}
	}

	private void Show()
	{
		TryShowWindow();
	}

	public static void TryShowWindow()
	{
		if (instance != null)
		{
			instance.StartCoroutine(instance.ShowWindow());
		}
	}

	public static void AddEndedProduct(string key)
	{
		int num = BCWStorage.Get<int>(keyCountPoductEndTime);
		BCWStorage.Set(keyPoductEndTime + num, key);
		BCWStorage.Set(keyCountPoductEndTime, num + 1);
	}

	private IEnumerator ShowWindow()
	{
		if (!BCWStorage.Get<bool>(keyNeedShowWindow))
		{
			yield break;
		}
		if (ReputationProductRewardWindow.instance != null)
		{
			while (ReputationProductRewardWindow.instance.isActive)
			{
				yield return null;
			}
		}
		TemporalProductManager.ClearProduct();
		productWeapon = MarketController.Instance.GetProduct("Weapons", "Rifles", "Famas_UP0");
		productArmor = MarketController.Instance.GetProduct("Armor", "Body", "keyArmor1");
		Product tempProduct2 = MarketController.Instance.GetProduct("Weapons", TutorialManager.weaponKey);
		if (MarketController.Instance.GetEquipedProduct(tempProduct2.categoryName, tempProduct2.subCategoryName).id == tempProduct2.id)
		{
			MarketController.Instance.UnEquip(tempProduct2);
			MarketController.Instance.EquipPurchasedProduct(tempProduct2.categoryName, tempProduct2.subCategoryName);
		}
		tempProduct2 = MarketController.Instance.GetProduct("Armor", TutorialManager.armorKey);
		if (MarketController.Instance.GetEquipedProduct(tempProduct2.categoryName, tempProduct2.subCategoryName) != null && MarketController.Instance.GetEquipedProduct(tempProduct2.categoryName, tempProduct2.subCategoryName).id == tempProduct2.id)
		{
			MarketController.Instance.UnEquip(tempProduct2);
			MarketController.Instance.EquipPurchasedProduct(tempProduct2.categoryName, tempProduct2.subCategoryName);
		}
		RefreshProducts();
		window.SetActive(true);
		yield return null;
	}

	private void RefreshProduct(Product product, UILabel lPrice, UILabel lName, UIButton buttonBuy, UIWidget equipped)
	{
		lPrice.text = product.Price.ToString();
		if (product.Price > settings.CathedExeptions)
		{
			lPrice.color = Color.red;
		}
		lName.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Market/" + product.id + "_title"))) ? ScriptLocalization.Get("Market/" + product.id + "_title") : string.Empty);
		if (product.Purchased)
		{
			buttonBuy.GetComponent<BoxCollider>().enabled = false;
			HOTween.To(buttonBuy.GetComponent<UIWidget>(), 0.5f, "alpha", 0);
			HOTween.To(equipped, 0.5f, "alpha", 1);
		}
		else
		{
			buttonBuy.GetComponent<BoxCollider>().enabled = true;
			buttonBuy.GetComponent<UIWidget>().alpha = 1f;
			equipped.alpha = 0f;
		}
	}

	private void RefreshProducts()
	{
		RefreshProduct(productWeapon, labelPriceWeapon, labelNameWeapon, buttonWeapon, weaponEquipped);
		RefreshProduct(productArmor, labelPriceArmor, labelNameArmor, buttonArmor, armorEquipped);
	}

	public void HideWindow()
	{
		window.SetActive(false);
		BCWStorage.Set(keyNeedShowWindow, false, true);
		StarterPacksManager.instance.UnlockStarterPack(StarterPacksManager.instance.endTutorEvent);
	}

	public void ShowWeapon()
	{
		MarketController.Instance.ShowMarket(productWeapon);
	}

	public void ShowArmor()
	{
		MarketController.Instance.ShowMarket(productArmor);
	}

	public void BuyWeapon()
	{
		BuyProduct(productWeapon);
	}

	public void BuyArmor()
	{
		BuyProduct(productArmor);
	}

	private void BuyProduct(Product product)
	{
		MarketController.Instance.BuyProduct(product);
		if (!product.Purchased)
		{
			HideWindow();
			StartCoroutine(MarketControllerUI.instance.ShowBank(product.PriceIn));
		}
		RefreshProducts();
	}

	public static void NeedShowWindow()
	{
		BCWStorage.Set(keyNeedShowWindow, true);
	}
}
